﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Krile.Kernel.Plugin;
using System.IO;
using Krile.Kernel.WindowsEx;

namespace Krile.Forms.PManForm
{
    public partial class PluginElement : UserControl
    {
        Cores.PluginData pdata;

        internal Cores.PluginData PluginData
        {
            get { return pdata; }
            set
            {
                pdata = value;
                if (String.IsNullOrEmpty(pdata.Name))
                {
                    pluginNameLabel.Text = Path.GetFileName(pdata.FilePath);
                    versionLabel.Text = String.Empty;
                    descriptionLabel.Text = pdata.Description;
                    pluginUrlLink.Enabled = false;
                    pluginUrlLink.Visible = false;
                }
                else
                {
                    pluginNameLabel.Text = pdata.Name;
                    versionLabel.Text = pdata.Version.ToString("0.00");
                    descriptionLabel.Text = pdata.Description;
                    if (pdata.SupportUri != null)
                    {
                        pluginUrlLink.Text = pdata.SupportUri.OriginalString;
                        pluginUrlLink.Enabled = true;
                        pluginUrlLink.Visible = true;
                    }
                    else
                    {
                        pluginUrlLink.Enabled = false;
                        pluginUrlLink.Visible = false;
                    }
                }
                enabledThisPlugin.Checked = value.Loadable;
            }
        }

        internal enum UpdateStates { Null, None, Exists, Error }

        private UpdateStates updState = UpdateStates.Null;

        internal UpdateStates UpdateState
        {
            get { return updState; }
            set
            {
                updState = value;
                updateLabel.Visible = value == UpdateStates.Exists;
                noUpdateLabel.Visible = value == UpdateStates.None;
                unsupportedUpdateLabel.Visible = value == UpdateStates.Error;
            }
        }

        internal PluginElement(Cores.PluginData relpdata)
        {
            InitializeComponent();
            PluginData = relpdata;
        }

        private void enabledThisPlugin_CheckedChanged(object sender, EventArgs e)
        {
            if (pdata.Loadable == enabledThisPlugin.Checked)
                return;
            if (Core.PluginManager.PluginAuth.SetAuthorize(pdata, enabledThisPlugin.Checked))
                MessageBoxEx.Show(Lang.Msg.PMan_PluginStateChanged, Lang.Msg.PMan_PluginStateChanged_Caption,
                                  MessageBoxEx.MessageBoxExButtons.OK, MessageBoxEx.MessageBoxExIcon.Information);
            else
                enabledThisPlugin.Checked = !enabledThisPlugin.Checked;
        }

        private void pluginUrlLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Subsystem.CommonOperation.CallBrowser(pluginUrlLink.Text);
        }
    }
}
